package org.lex.notepad.ui.status;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.nio.charset.Charset;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import org.lex.swing.widgets.MemoryBar;


public class StatusPane extends JPanel implements StatusBar {
	private JLabel locationLabel = null;
	private JComponent sepLabel2 = null;
	private JLabel formatLabel = null;
	private JLabel charsetLabel = null;
	private JComponent sepLabel3 = null;
	private JComponent sepLabel4 = null;
	private MemoryBar memoryBar = null;

	public StatusPane() {
		super();
		initialize();
	}

	public void setCharset(Charset charset) {
		charsetLabel.setText(charset.displayName());
	}

	public void setFileFormat(String format) {
		formatLabel.setText(format);
	}

	private StringBuilder buffer = new StringBuilder();

	public void setPosition(int line, int column) {
		buffer.setLength(0);
		buffer.append(line).append(':').append(column);
		locationLabel.setText(buffer.toString());
	}

	public void setPositionEnabled(boolean b) {
		if (false == b) {
			locationLabel.setText("");
		}
	}

	private void initialize() {
		GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
		gridBagConstraints8.gridx = 6;
		gridBagConstraints8.ipadx = 0;
		gridBagConstraints8.insets = new Insets(3, 0, 3, 5);
		gridBagConstraints8.gridy = 0;
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 5;
		gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints7.weighty = 1.0;
		gridBagConstraints7.insets = new Insets(5, 6, 5, 6);
		gridBagConstraints7.gridy = 0;
		sepLabel4 = new JSeparator(JSeparator.VERTICAL);
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 3;
		gridBagConstraints6.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints6.weighty = 1.0;
		gridBagConstraints6.insets = new Insets(5, 6, 5, 6);
		gridBagConstraints6.gridy = 0;
		sepLabel3 = new JSeparator(JSeparator.VERTICAL);
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 4;
		gridBagConstraints5.gridy = 0;
		charsetLabel = new JLabel();
		charsetLabel.setText(Charset.defaultCharset().displayName());
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 2;
		gridBagConstraints4.gridy = 0;
		formatLabel = new JLabel();
		formatLabel.setText("Unkown");
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 1;
		gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints3.weighty = 1.0;
		gridBagConstraints3.insets = new Insets(5, 6, 5, 6);
		gridBagConstraints3.gridy = 0;
		sepLabel2 = new JSeparator(JSeparator.VERTICAL);
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.anchor = GridBagConstraints.WEST;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints2.gridy = 0;
		locationLabel = new JLabel();
		locationLabel.setText("0:0");
		this.setSize(350, 25);
		this.setLayout(new GridBagLayout());
		this.add(locationLabel, gridBagConstraints2);
		this.add(sepLabel2, gridBagConstraints3);
		this.add(formatLabel, gridBagConstraints4);
		this.add(sepLabel3, gridBagConstraints6);
		this.add(charsetLabel, gridBagConstraints5);
		this.add(sepLabel4, gridBagConstraints7);
		this.add(getMemoryBar(), gridBagConstraints8);
	}

	private MemoryBar getMemoryBar() {
		if (memoryBar == null) {
			memoryBar = new MemoryBar();
			memoryBar.setShowPercent(false);
			memoryBar.setPreferredSize(new Dimension(100, 20));
		}
		return memoryBar;
	}
}
